package com.ruoyi.web.controller.tzzl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.tzzl.api.TheMapApi;
import com.ruoyi.tzzl.domain.WzDynamic;
import com.ruoyi.tzzl.domain.WzGiveLike;
import com.ruoyi.tzzl.domain.vo.QueryVo;
import com.ruoyi.tzzl.service.IWzDynamicService;
import com.ruoyi.tzzl.service.IWzGiveLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户动态Controller
 * @author by caoyong
 * @date 2022/11/18
 **/
@RestController
@RequestMapping("/tzzl/dynamic")
public class WzDynamicController extends BaseController {

    @Autowired
    private IWzDynamicService wzDynamicService;
    @Autowired
    private IWzGiveLikeService wzGiveLikeService;

    /**
     * 查看用户动态列表
     * */
    @GetMapping("/list")
    public AjaxResult list(Long id,Long wid,Long uid,String type){
        return AjaxResult.success(wzDynamicService.getByWid(id,wid,uid,type));
    }

    /**发布动态*/
    @PostMapping("/release")
    public AjaxResult release(@RequestBody WzDynamic wzDynamic){
               wzDynamic.setCreateTime(new Date());
               //位置
               if (ObjectUtils.isNotEmpty(wzDynamic.getLocation())){
                 String location =  TheMapApi.getLatAndLngByAddr(wzDynamic.getLocation());
                   String longitude = location.substring(0,location.indexOf(","));//经度
                   String latitude =location.substring(longitude.length()+1,location.length()); //维度
                   wzDynamic.setLongitude(longitude);
                   wzDynamic.setDimensionality(latitude);
               }
        return AjaxResult.success(wzDynamicService.save(wzDynamic));
    }

    /**点赞*/
    @PostMapping("/giveALike")
    @RepeatSubmit
    public AjaxResult giveALike(@RequestBody QueryVo queryVo){

        return AjaxResult.success(wzGiveLikeService.giveAlike(queryVo));
    }

    /**
     * 取消点赞
     * */
    @PostMapping("/cancel")
    @RepeatSubmit
    public AjaxResult cancel(@RequestBody QueryVo queryVo){
        return AjaxResult.success(wzGiveLikeService.cancel(queryVo));
    }

    /**删除动态*/
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody QueryVo queryVo){
        return AjaxResult.success(wzDynamicService.removeById(queryVo.getId()));
    }

    /**判断某条评论自己是否点了赞*/
    @GetMapping("/judge")
    public AjaxResult judge(Long id,Long wid){
        return AjaxResult.success(wzGiveLikeService.judge(id,wid));
    }



}
